package net.sk.china.platform.web;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sk.china.platform.model.wechat.WechatReply;
import net.sk.china.platform.request.wechat.WechatReplyRequest;
import net.sk.china.platform.request.wechat.WechatReplySearchRequest;
import net.sk.china.platform.service.wechat.WechatReplyService;
import net.sk.china.platform.service.wechat.WeixinPublicService;
import net.sk.china.common.request.PageParamRequest;
import net.sk.china.common.response.CommonPage;
import net.sk.china.common.response.CommonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/2 10:18
 * // +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("wechat/")
@Api(tags = "微信开放平台")
public class WeixinController {

    private final WeixinPublicService weixinPublicService;

    private final WechatReplyService wechatReplyService;

    public WeixinController(WeixinPublicService weixinPublicService, WechatReplyService wechatReplyService) {
        this.weixinPublicService = weixinPublicService;
        this.wechatReplyService = wechatReplyService;
    }

    /**
     * 获取微信菜单
     */
    @PreAuthorize("hasAuthority('admin:wechat:menu:public:get')")
    @ApiOperation(value = "获取自定义菜单")
    @RequestMapping(value = "menu/public/get", method = RequestMethod.GET)
    public CommonResult<Object> get() {
        return CommonResult.success(weixinPublicService.getCustomizeMenus());
    }

    /**
     * 创建微信菜单
     * @param data 菜单数据，具体json格式参考微信开放平台
     */
    @PreAuthorize("hasAuthority('admin:wechat:menu:public:create')")
    @ApiOperation(value = "保存自定义菜单")
    @RequestMapping(value = "menu/public/create", method = RequestMethod.POST)
    public CommonResult<JSONObject> create(@RequestBody String data) {
        if (weixinPublicService.createMenus(data)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 删除微信菜单
     */
    @PreAuthorize("hasAuthority('admin:wechat:menu:public:delete')")
    @ApiOperation(value = "删除自定义菜单")
    @RequestMapping(value = "menu/public/delete", method = RequestMethod.GET)
    public CommonResult<JSONObject> delete() {
        if (weixinPublicService.deleteMenus()) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @PreAuthorize("hasAuthority('admin:wechat:keywords:reply:list')")
    @ApiOperation(value = "关键字回复列表")
    @RequestMapping(value = "keywords/reply/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<WechatReply>>  getList(@Validated WechatReplySearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<WechatReply> wechatReplyCommonPage = CommonPage.restPage(wechatReplyService.getList(request, pageParamRequest));
        return CommonResult.success(wechatReplyCommonPage);
    }


    @PreAuthorize("hasAuthority('admin:wechat:keywords:reply:save')")
    @ApiOperation(value = "新增关键字")
    @RequestMapping(value = "keywords/reply/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated WechatReplyRequest wechatReplyRequest) {
        WechatReply wechatReply = new WechatReply();
        BeanUtils.copyProperties(wechatReplyRequest, wechatReply);
        if (wechatReplyService.create(wechatReply)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }


    @PreAuthorize("hasAuthority('admin:wechat:keywords:reply:delete')")
    @ApiOperation(value = "删除关键")
    @RequestMapping(value = "keywords/reply/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        if (wechatReplyService.removeById(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PreAuthorize("hasAuthority('admin:wechat:keywords:reply:update')")
    @ApiOperation(value = "修改关键字")
    @RequestMapping(value = "keywords/reply/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated WechatReplyRequest wechatReplyRequest) {
        if (wechatReplyService.updateReply(wechatReplyRequest)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PreAuthorize("hasAuthority('admin:wechat:keywords:reply:status')")
    @ApiOperation(value = "修改状态")
    @RequestMapping(value = "keywords/reply/status", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestParam(value = "id") Integer id, @RequestParam(value = "status") Boolean status) {
        if (wechatReplyService.updateStatus(id, status)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @PreAuthorize("hasAuthority('admin:wechat:keywords:reply:info')")
    @ApiOperation(value = "查看关键字详情")
    @RequestMapping(value = "keywords/reply/info", method = RequestMethod.GET)
    public CommonResult<WechatReply> info(@RequestParam(value = "id") Integer id) {
        WechatReply wechatReply = wechatReplyService.getInfo(id);
        return CommonResult.success(wechatReply);
    }

    @PreAuthorize("hasAuthority('admin:wechat:keywords:reply:info:keywords')")
    @ApiOperation(value = "根据关键字查询数据")
    @RequestMapping(value = "keywords/reply/info/keywords", method = RequestMethod.GET)
    public CommonResult<WechatReply> info(@RequestParam(value = "keywords") String keywords) {
        WechatReply wechatReply = wechatReplyService.getVoByKeywords(keywords);
        return CommonResult.success(wechatReply);
    }
}
